package easy;

import java.util.LinkedList;

public class ValidParentheses {
	public boolean isValid(String s) {
		LinkedList<Character> arrayList = new LinkedList<Character>();
		boolean isV = true;
		int top = -1;
		for (int i = 0; i < s.length(); i++) {
			if (s.charAt(i) == '{' || s.charAt(i) == '[' || s.charAt(i) == '(')
			{
				arrayList.add(++top,s.charAt(i));
			}
			try {
				if (s.charAt(i) == '}') {
					char c = arrayList.get(top--);
					if (c != '{')
						isV = false;
				}
				if (s.charAt(i) == ']') {
					char c = arrayList.get(top--);
					if (c != '[')
						isV = false;
				}
				if (s.charAt(i) == ')') {
					char c = arrayList.get(top--);
					if (c != '(')
						isV = false;
				}
				System.out.println(top);
			} catch (Exception e) {
				// TODO: handle exception
				isV = false;
				break;
			}
		}
		if(top != -1)
			isV = false;
		return isV;
	}
}
